package com.jsyl.lwbk.business.service.impl;

import com.jsyl.lwbk.business.domain.ClassPlanDetail;
import com.jsyl.lwbk.business.domain.TrainPackage;
import com.jsyl.lwbk.business.domain.Integration;
import com.jsyl.lwbk.business.dto.SaveProjectDTO;
import com.jsyl.lwbk.business.dto.TrainPackageDTO;
import com.jsyl.lwbk.business.mapper.ClassPlanDetailMapper;
import com.jsyl.lwbk.business.mapper.TrainPackageMapper;
import com.jsyl.lwbk.business.mapper.IntegrationMapper;
import com.jsyl.lwbk.business.service.TrainPackageService;
import com.jsyl.lwbk.common.core.domain.Ztree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Fitch
 * 2022/2/21
 */
@Service
public class TrainPackageServiceImpl implements TrainPackageService {
    @Autowired
    private TrainPackageMapper trainPackageMapper;
    @Autowired
    private ClassPlanDetailMapper classPlanDetailMapper;
    @Autowired
    private IntegrationMapper integrationMapper;

    @Override
    public List<TrainPackageDTO> findList(TrainPackageDTO dto) {
        List<TrainPackageDTO> list = this.trainPackageMapper.findList(dto.getAgeGrade(),dto.getCourseId());
        Map<String, String> codeName =getCodeName();
        for(TrainPackageDTO c:list){
            if(c.getProjectCode() !=null){
                String name  = c.getProjectCode();
                String[] projectCodes= c.getProjectCode().split(",");
                for(String s:projectCodes){
                    name= name.replace(s,codeName.get(s));
                }
                c.setProjectName(name);
            }

        }
        return list;
    }

    @Override
    public List<TrainPackageDTO> detailList(Long trainPackageId) {
        return this.trainPackageMapper.detailList(trainPackageId);
    }

    private Map<String, String> getCodeName() {
        List<Integration> list = integrationMapper.findList(null);
        Map<String,String> result =new HashMap<>();
        for(Integration i:list){
            result.put(i.getCode(),i.getName());
        }
        return result;
    }
    @Override
    public List<Ztree> trainPackageTree(Long classPlanDetailId) {
        ClassPlanDetail classPlanDetail =this.classPlanDetailMapper.getBaseInfo(classPlanDetailId);
        List<TrainPackageDTO> list = this.trainPackageMapper.findList(classPlanDetail.getAgeGrade(),classPlanDetail.getCourseId());
        List<String> checkedList = new ArrayList<>();
        if (classPlanDetail != null && classPlanDetail.getTrainPackageId() != null) {
            checkedList = CollectionUtils.arrayToList(classPlanDetail.getTrainPackageId().split(","));
        }
        return initZtree(list,checkedList,false);
    }
    public List<Ztree> initZtree(List<TrainPackageDTO> menuList, List<String> checkedList, boolean permsFlag) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        boolean isCheck = CollectionUtils.isEmpty(checkedList);
        for (TrainPackageDTO menu : menuList) {
            Ztree ztree = new Ztree();
            ztree.setId(Long.valueOf(menu.getId()));
            ztree.setpId(Long.valueOf(menu.getParentTrainId()));
            ztree.setName(menu.getTrainName());
            ztree.setTitle(menu.getTrainName());
            if (!isCheck) {
                ztree.setChecked(checkedList.contains(menu.getId().toString()));
            }
            ztrees.add(ztree);
        }
        return ztrees;
    }


    @Override
    public List<TrainPackageDTO> getParentTrainPackage() {
        return this.trainPackageMapper.getParentTrainPackage();
    }

    @Override
    public int checkTrainNameUnique(TrainPackage trainPackage) {
        return this.trainPackageMapper.checkTrainNameUnique(trainPackage.getTrainName())>0?1:0;
    }



    @Override
    public int saveProject(SaveProjectDTO dto) {
        TrainPackage trainPackage = this.trainPackageMapper.getById(Long.valueOf(dto.getTrainPackageId()));
        if (trainPackage == null) {
            return 0;
        }
        //保存项目信息
        trainPackage.setProjectCode(null);
        if (dto.getIntegrationIds() != null) {
            String projectCode = this.integrationMapper.getCodeById(CollectionUtils.arrayToList(dto.getIntegrationIds().split(",")));
            trainPackage.setProjectCode(projectCode);
        }
        trainPackage.setUpdateUser(dto.getUpdateUser());
        this.trainPackageMapper.update(trainPackage);
        return 1;
    }

    @Override
    public int addTrainPackage(TrainPackage trainPackage) {
        if(trainPackage.getParentTrainId() ==null){
            trainPackage.setParentTrainId(0L);
        }
//        trainPackage.setId(IdWorker.getId());
        return this.trainPackageMapper.insert(trainPackage);
    }

    @Override
    public int deleteById(String id) {
        return this.trainPackageMapper.deleteById(id);
    }
}
